package com.tinyseed.emcp.api.basicdata.rest;

import com.tinyseed.emcp.api.app.common.DateUtil;
import com.tinyseed.emcp.api.app.config.DataSourceKey;
import com.tinyseed.emcp.api.app.config.TargetDataSource;
import com.tinyseed.emcp.api.basicdata.model.EnterpriseInputDTO;
import com.tinyseed.emcp.api.basicdata.service.EnterpriseMonitorService;
import com.tinyseed.hmxx.common.base.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

/***
 * @ClassName: EnterpriseMonitorController
 * @Description: 企业监控
 * @Auther: Harlan.Xiong
 * @Date: 2019-12-27 15:08
 * @version : V1.0
 */

@Slf4j
@RestController
@RequestMapping("/enterpriseMonitor")
public class EnterpriseMonitorController extends BaseController {

    @Autowired
    private EnterpriseMonitorService enterpriseMonitorService;

    @GetMapping(value = "/getTree")
    @ApiOperation("查询公司检测点树")
    public ResponseEntity getTree(Long id) {
        return new ResponseEntity<>(enterpriseMonitorService.getTree(id), HttpStatus.OK);

    }

    @GetMapping(value = "getDeviceInfoList")
    @ApiOperation("分页查询")
    public ResponseEntity getDeviceInfoList(EnterpriseInputDTO enterpriseInputDTO, int limit, int page) {
        return new ResponseEntity<>(enterpriseMonitorService.findAll(enterpriseInputDTO, limit, page), HttpStatus.OK);

    }

    @GetMapping(value = "getDeviceDataLatest")
    @ApiOperation("查询设备最新数据")
    public ResponseEntity getDeviceDataLatest(String id) {
        return new ResponseEntity<>(enterpriseMonitorService.getDeviceDataLatest(id), HttpStatus.OK);

    }

    @GetMapping(value = "queryDeviceDataListChart")
    @ApiOperation("查询设备数据")
    public ResponseEntity queryDeviceDataListChart(String id, String dataTime) {
        Date date = null;
        if(dataTime.length()!=0){
            date = DateUtil.parseDateUTCTime(dataTime);
        }else{
            LocalDate localDate = LocalDate.now();
            localDate = localDate.minusDays(1);
            ZoneId zone = ZoneId.systemDefault();
            Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
            date = Date.from(instant);
        }
        return new ResponseEntity<>(enterpriseMonitorService.queryDeviceDataListChart(id, date), HttpStatus.OK);
    }

    @GetMapping(value = "getEnterpriseMonitorInfo")
    @ApiOperation("查询企业信息")
    public ResponseEntity getEnterpriseMonitorInfo(Long id) {
        return new ResponseEntity<>(enterpriseMonitorService.getEnterpriseMonitorInfo(id), HttpStatus.OK);

    }
}
